//
// CLiveCapture.cpp
//

/*-----------------------------------------------------*\
			HQ Tech, Make Technology Easy!       
 More information, please go to http://hqtech.nease.net.
/*-----------------------------------------------------*/

#include "stdafx.h"
#include "CLiveCapture.h"

#include "GlobalDefs.h"
#include "CDSDevice.h"
#include "CVideoDevices.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

///////////////////////////////////////////////////////////////////////////
CLiveCapture::CLiveCapture()
{
	mMode         = MD_Preview;
	mNotifying    = TRUE;
	mVMRPreferred = FALSE; // Must be inited to FALSE!
	mGrabEnabled  = TRUE;

	mVideoDevice    = "";
	mAudioDevice    = "";
	mDeviceType     = DT_Unknown;
	mResolution     = SR_PAL;
	mVideoConnector = 2;   // AV Composite
	mAudioConnector = -1;  // Need outer initialzing
	mAudioMixLevel  = 1;

	mEncodingType = ET_AVI;
	mOutputWidth  = 320;
	mOutputHeight = 240;
	mOutputFile   = "C:\\sample.avi";
	mGrabFolder   = "C:\\";
}

CLiveCapture::~CLiveCapture()
{
}

void CLiveCapture::SetWorkMode(Work_Mode inMode)
{
	mMode = inMode;
}

Work_Mode CLiveCapture::GetWorkMode(void)
{
	return mMode;
}

void CLiveCapture::SetNotifying(BOOL inEnabled)
{
	mNotifying = inEnabled;
}

void CLiveCapture::SetVMRPreferred(BOOL inPreferred)
{
	mVMRPreferred = inPreferred;
}

BOOL CLiveCapture::IsVMRPreferred(void)
{
	return mVMRPreferred;
}

void CLiveCapture::SetImageGrab(BOOL inEnabled)
{
	mGrabEnabled = inEnabled;
}

BOOL CLiveCapture::IsGrabEnabled(void)
{
	return mGrabEnabled;
}

void CLiveCapture::SetVideoDevice(const char * inDevice)
{
	if (mVideoDevice.Compare(inDevice) != 0)
	{
		mVideoDevice = inDevice; 
		// Auto update the device type
		DecideDeviceType(inDevice);
		if (mNotifying)
		{
			Notify(cVideoSourceChanged);
		}
	}	
}

void CLiveCapture::DecideDeviceType(const char * inName)
{
	CDSDevice device;
	if (CVideoDevices::Instance()->FindDevice(inName, device))
	{
		SetDeviceType(device.GetDeviceType());
	}
	else
	{
		SetDeviceType(DT_Unknown);
	}
}

CString& CLiveCapture::GetVideoDevice(void)
{
	return mVideoDevice;
}

void CLiveCapture::SetDeviceType(Device_Type inType)
{
	mDeviceType = inType;
}

Device_Type CLiveCapture::GetDeviceType(void)
{
	return mDeviceType;
}

void CLiveCapture::SetAudioDevice(const char * inDevice)
{
	if (mAudioDevice.Compare(inDevice) != 0)
	{
		mAudioDevice = inDevice; 
		if (mNotifying)
		{
			Notify(cAudioSourceChanged);
		}
	}
}

CString& CLiveCapture::GetAudioDevice(void)
{
	return mAudioDevice;
}

void CLiveCapture::SetVideoConnector(long inConnector)
{
	if (mVideoConnector != inConnector)
	{
		mVideoConnector = inConnector;
		if (mNotifying)
		{
			Notify(cVideoConnectorChanged);
		}
	}
}

long CLiveCapture::GetVideoConnector(void)
{
	return mVideoConnector;
}

void CLiveCapture::SetVideoResolution(Signal_Resolution inResolution)
{
	if (mResolution != inResolution)
	{
		mResolution = inResolution;
		if (mNotifying)
		{
			Notify(cVideoResolutionChanged);
		}
	}
}

Signal_Resolution CLiveCapture::GetVideoResolution()
{
	return mResolution;
}

void CLiveCapture::SetAudioConnector(long inConnector)
{
	if (mAudioConnector != inConnector)
	{
		mAudioConnector = inConnector;
		if (mNotifying)
		{
			Notify(cAudioConnectorChanged);
		}
	}
}

long CLiveCapture::GetAudioConnector(void)
{
	return mAudioConnector;
}

BOOL CLiveCapture::IsAudioConnectorValid(void)
{
	return (mAudioConnector >= 0);
}

void CLiveCapture::SetAudioMixLevel(double inLevel)
{
	if (mAudioMixLevel != inLevel)
	{
		mAudioMixLevel = inLevel;
		if (mNotifying)
		{
			Notify(cAudioMixLevelChanged);
		}
	}
}

double CLiveCapture::GetAudioMixLevel(void)
{
	return mAudioMixLevel;
}

void CLiveCapture::SetEncodingType(Encoding_Type inType)
{
	mEncodingType = inType;
	ValidateEncodingType();
}

// To assure encoder can do real-time encoding,
// never set output image size too large, especailly DIVX.
void CLiveCapture::ValidateEncodingType(void)
{
	if (mEncodingType == ET_DIVX)
	{
		mOutputWidth  = 320;
		mOutputHeight = 240;
	}
}

Encoding_Type CLiveCapture::GetEncodingType(void)
{
	return mEncodingType;
}

void CLiveCapture::SetImageSize(long inWidth, long inHeight)
{
	mOutputWidth  = inWidth;
	mOutputHeight = inHeight;
}

void CLiveCapture::GetImageSize(long * outWidth, long * outHeight)
{
	if (outWidth && outHeight)
	{
		*outWidth  = mOutputWidth;
		*outHeight = mOutputHeight;
	}
}

void CLiveCapture::SetOutputFile(const char * inFile)
{
	mOutputFile = inFile;
}

CString& CLiveCapture::GetOutputFile(void)
{
	return mOutputFile;
}

void CLiveCapture::SetGrabFolder(const char * inFolder)
{
	mGrabFolder = inFolder;
}

CString& CLiveCapture::GetGrabFolder(void)
{
	return mGrabFolder;
}